package com.zhkj.txg.module.home.adapter

import android.graphics.Color
import android.util.Log
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.RadioButton
import android.widget.TextView
import androidx.core.content.ContextCompat
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.zhkj.lib.config.BASE_URL
import com.zhkj.txg.R
import com.zhkj.txg.module.home.entity.PromotionInfo
import com.zhkj.txg.module.home.entity.RecommendInfo
import com.zhkj.txg.module.home.entity.ShopCateSecondEntity
import com.zhkj.txg.module.home.entity.ShopCategoryEntity
import com.zhkj.txg.utils.ImageUtils

class CategoryLeftAdapter(var params: Int = -1, val onItemClick: (shopCateSecond: ShopCategoryEntity) -> Unit) :
    BaseQuickAdapter<ShopCategoryEntity, BaseViewHolder>(R.layout.item_cate_left) {

    var index: Int = -1

    init {
        setOnItemClickListener { _, _, position ->
            val flCategoryLeft = getViewByPosition(position, R.id.flCategoryLeft)
                    as FrameLayout
            flCategoryLeft.setBackgroundColor(Color.WHITE)

            val tvCateName = getViewByPosition(position, R.id.tvCateName)
                    as TextView
            tvCateName.setTextColor(ContextCompat.getColor(context, R.color.colorMain))

            index = position
            notifyDataSetChanged()
            params = 0
            onItemClick(data[position])
        }
    }

    override fun convert(helper: BaseViewHolder, item: ShopCategoryEntity) {
        helper.setText(R.id.tvCateName, item.name)
        val flCategoryLeft = helper.getView<FrameLayout>(R.id.flCategoryLeft)
        val tvCateName = helper.getView<TextView>(R.id.tvCateName)
        val tvCateLine = helper.getView<TextView>(R.id.tvCateLine)
        if (params == item.id) {
            index = getItemPosition(item)
        } else if (params == -1) {
            index = 0
        }
        if (index == getItemPosition(item)) {
            onItemClick(item)
            flCategoryLeft.setBackgroundColor(Color.WHITE)
            tvCateName.setTextColor(ContextCompat.getColor(context, R.color.colorMain))
            tvCateLine.setBackgroundResource(R.color.colorMain)
        } else {
            flCategoryLeft.setBackgroundColor(Color.TRANSPARENT)
            tvCateName.setTextColor(Color.parseColor("#FF666666"))
            tvCateLine.setBackgroundColor(Color.TRANSPARENT)
        }
    }
}